﻿// -----------------------------------------------------------------------
// <copyright file="PostProcessor.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.Core
{
    using Model;

    /// <summary>
    /// Utility class to apply post processing operations
    /// </summary>
    public static class PostProcessor
    {
        /// <summary>
        /// Cuts off the pixels.
        /// </summary>
        /// <param name="pixelDoubles">The pixel doubles.</param>
        /// <returns>Array of pixels after cutoff</returns>
        public static Pixel[,] CutOff(PixelDouble[,] pixelDoubles)
        {
            Pixel[,] pixels = new Pixel[pixelDoubles.GetLength(0), pixelDoubles.GetLength(1)];
            for (int i = 0; i < pixelDoubles.GetLength(0); i++)
            {
                for (int j = 0; j < pixelDoubles.GetLength(1); j++)
                {
                    pixels[i, j] = pixelDoubles[i, j].CutOff();
                }
            }

            return pixels;
        }

        /// <summary>
        /// Absolute and Cuts off the pixels.
        /// </summary>
        /// <param name="pixelDoubles">The pixel doubles.</param>
        /// <returns>Array of pixels after absolute and cutoff</returns>
        public static Pixel[,] Absolute(PixelDouble[,] pixelDoubles)
        {
            Pixel[,] pixels = new Pixel[pixelDoubles.GetLength(0), pixelDoubles.GetLength(1)];
            for (int i = 0; i < pixelDoubles.GetLength(0); i++)
            {
                for (int j = 0; j < pixelDoubles.GetLength(1); j++)
                {
                    pixels[i, j] = pixelDoubles[i, j].Absolute().CutOff();
                }
            }

            return pixels;
        }

        /// <summary>
        /// Normalizes the specified pixel doubles.
        /// </summary>
        /// <param name="pixelDoubles">The pixel doubles.</param>
        /// <param name="from">Minimum of the new range</param>
        /// <param name="to">Maximum of the new range</param>
        /// <returns>Normalized pixels</returns>
        public static PixelDouble[,] Normalize(PixelDouble[,] pixelDoubles, PixelDouble from, PixelDouble to)
        {
            PixelDouble minValues = new PixelDouble(double.MaxValue, double.MaxValue, double.MaxValue);
            PixelDouble maxValues = new PixelDouble(double.MinValue, double.MinValue, double.MinValue);
            PixelDouble tmpDouble;

            int width = pixelDoubles.GetLength(0);
            int height = pixelDoubles.GetLength(1);

            PixelDouble[,] normalizedPixels = new PixelDouble[width, height];
            PixelDouble normalizedPixel;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    tmpDouble = pixelDoubles[i, j];
                    if (tmpDouble.Red < minValues.Red)
                    {
                        minValues.Red = tmpDouble.Red;
                    }

                    if (tmpDouble.Green < minValues.Green)
                    {
                        minValues.Green = tmpDouble.Green;
                    }

                    if (tmpDouble.Blue < minValues.Blue)
                    {
                        minValues.Blue = tmpDouble.Blue;
                    }

                    if (tmpDouble.Red > maxValues.Red)
                    {
                        maxValues.Red = tmpDouble.Red;
                    }

                    if (tmpDouble.Green > maxValues.Green)
                    {
                        maxValues.Green = tmpDouble.Green;
                    }

                    if (tmpDouble.Blue > maxValues.Blue)
                    {
                        maxValues.Blue = tmpDouble.Blue;
                    }
                }
            }

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    normalizedPixel = to + ((from - to) * ((pixelDoubles[i, j] - minValues) / (maxValues - minValues)));

                    normalizedPixels[i, j] = normalizedPixel;
                }
            }

            return normalizedPixels;
        }
    }
}
